package com.rxt.thermopro.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.rxt.thermopro.R
import com.rxt.thermopro.bean.ContactBean
import com.rxt.thermopro.bean.DeviceBean
import kotlinx.android.synthetic.main.item_view_contact_header.view.*
import kotlinx.android.synthetic.main.item_view_contact_list.view.*
import kotlinx.android.synthetic.main.item_view_contact_list.view.contactNum
import kotlinx.android.synthetic.main.item_view_contact_list.view.viewIcon
import kotlinx.android.synthetic.main.item_view_device_list.view.*

class ContactListAdapter(var list: MutableList<ContactBean>) :
    RecyclerView.Adapter<ContactListAdapter.MyViewHolder>() {

    companion object {
        const val VIEW_TYPE_HEADER_EMAIL = 1
        const val VIEW_TYPE_HEADER_PHONE = 2
        const val VIEW_TYPE_VIEW = 3
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    var onClick: OnItemClickListener? = null

    fun setOnItemClick(onItemClickListener: OnItemClickListener) {
        this.onClick = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return if (viewType == VIEW_TYPE_VIEW) {
            val itemView =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_view_contact_list, parent, false)
            MyViewHolder(itemView)
        } else {
            val itemView =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_view_contact_header, parent, false)
            MyViewHolder(itemView)
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return list[position].viewType
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if (list[position].viewType == VIEW_TYPE_VIEW) {
            holder.itemView.viewIcon.setImageResource(list[position].viewIcon)
            holder.itemView.contactNum.text = list[position].contactNumber
            holder.itemView.contactTime.text = list[position].contactTime
        } else {
            holder.itemView.viewIcon.setImageResource(list[position].viewIcon)
            holder.itemView.contactText.text = list[position].contactNumber
        }
        holder.itemView.setOnClickListener {
            onClick?.onItemClick(holder.itemView, position)
        }
    }

    fun add(bean: ContactBean) {
        Log.d("112121->adapter", bean.contactNumber + "\n" + bean.contactTime)
        list.add(bean)
        notifyDataSetChanged()
    }

    fun addAll(dataList: List<ContactBean>) {
        list.addAll(dataList)
        notifyDataSetChanged()
    }

    fun clear() {
        list.clear()
        notifyDataSetChanged()
    }

    fun get(index: Int): ContactBean {
        return list[index]
    }

    fun hasBlueDevice(contactNumber: String?): Boolean {
        var flag = -1
        list.forEach {
            if (it.contactNumber == contactNumber) {
                flag = 1
            }
        }
        return flag == 1
    }

    fun update(itemBean: ContactBean?) {
        itemBean?.let {
            list.forEach {
                if (it.contactNumber == itemBean.contactNumber) {
                    it.contactTime = itemBean.contactTime
                    it.viewIcon = itemBean.viewIcon
                }
            }
        }
        notifyDataSetChanged()
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

}